package com.tledu.email.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.tledu.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tledu.common.annotation.Log;
import com.tledu.common.core.controller.BaseController;
import com.tledu.common.core.domain.AjaxResult;
import com.tledu.common.enums.BusinessType;
import com.tledu.email.domain.EmailWrite;
import com.tledu.email.service.IEmailWriteService;
import com.tledu.common.utils.poi.ExcelUtil;
import com.tledu.common.core.page.TableDataInfo;

/**
 * 写信Controller
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@RestController
@RequestMapping("/email/write")
public class EmailWriteController extends BaseController {
    @Autowired
    private IEmailWriteService emailWriteService;

    /**
     * send按钮点击发送
     * 新增写信
     */
    @PreAuthorize("@ss.hasPermi('email:write:send')")
    @Log(title = "写信", businessType = BusinessType.INSERT)
    @PostMapping("/send")
    public AjaxResult insertEmailWrite(@RequestBody EmailWrite emailWrite) {
        if (emailWrite.getProject() != null && emailWrite.getRecipientsId() != null && emailWrite.getRecipientsId() != SecurityUtils.getUserId() && emailWrite.getType() != null && emailWrite.getContent() != null) {
            return toAjax(emailWriteService.insertEmailWrite(emailWrite));
        } else if(emailWrite.getProject() == null || emailWrite.getRecipientsId() == null || emailWrite.getRecipientsId() == null || emailWrite.getType() == null || emailWrite.getContent() == null) {
            return AjaxResult.error("参数缺失，请检查project, recipientsId, type, content是否都已填写。");
        }else {
            return AjaxResult.error("不能给自己发邮件");
        }
    }

    // 存为草稿操作，发送一个json格式的字符串
    @PreAuthorize("@ss.hasPermi('email:write:draft')")
    @Log(title = "存为草稿", businessType = BusinessType.INSERT)
    @PostMapping("/draft")
    public AjaxResult draft(@RequestBody EmailWrite emailWrite) {
        if (emailWrite.getProject() != null && emailWrite.getRecipientsId() != null && emailWrite.getType() != null && emailWrite.getContent() != null) {
            return toAjax(emailWriteService.saveAsDraft(emailWrite));
        } else {
            return AjaxResult.error("参数缺失，请检查project, recipientsId, type, content是否都已填写。");
        }
    }

    /**
     * 查询写信列表
     */
    @PreAuthorize("@ss.hasPermi('email:write:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmailWrite emailWrite) {
        startPage();
        List<EmailWrite> list = emailWriteService.selectEmailWriteList(emailWrite);
        return getDataTable(list);
    }

    /**
     * 导出写信列表
     */
    @PreAuthorize("@ss.hasPermi('email:write:export')")
    @Log(title = "写信", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmailWrite emailWrite) {
        List<EmailWrite> list = emailWriteService.selectEmailWriteList(emailWrite);
        ExcelUtil<EmailWrite> util = new ExcelUtil<EmailWrite>(EmailWrite.class);
        util.exportExcel(response, list, "写信数据");
    }

    /**
     * 获取写信详细信息
     */
    @PreAuthorize("@ss.hasPermi('email:write:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(emailWriteService.selectEmailWriteById(id));
    }


    /**
     * 修改写信
     */
    @PreAuthorize("@ss.hasPermi('email:write:edit')")
    @Log(title = "写信", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmailWrite emailWrite) {
        return toAjax(emailWriteService.updateEmailWrite(emailWrite));
    }

    /**
     * 删除写信
     */
    @PreAuthorize("@ss.hasPermi('email:write:remove')")
    @Log(title = "写信", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(emailWriteService.deleteEmailWriteByIds(ids));
    }
}
